<?php

/*
 * 2021/7/16
 * 8:59
 * User: 2650332702@qq.com
 */

namespace App\Api\Repositories;

use App\Api\Bases\BaseController;
use App\Api\Manages\CommonController;
use App\Api\Models\Comments;
use App\Api\Models\Goods;
use App\Api\Models\GoodsComments;
use App\Api\Models\Seckill;
use App\Api\Models\SeckillOrder;
use App\Api\Services\Seckill as ServicesSeckill;
use Illuminate\Support\Facades\Redis;

class SeckillRepositorie
{
    static public function equivalent()
    {
        $time = (new BaseController())->NowTime();
        //获得秒杀商品数量
        $mcount = Seckill::getCount($time);
        $rcount = Redis::hlen('seckilllist');
        if ($mcount == $rcount) {
            return true;
        } else {
            return false;
        }
    }

    //秒杀商品列表
    static public function seckillAll($allow, $condition)
    {
        $time = (new BaseController())->NowTime();
        $equivalent = self::equivalent();
        $redislist = [];
        if (!$equivalent) {
            Redis::del('seckilllist');
            $seckills = Seckill::dataSorting($allow, $condition, $time);
            foreach ($seckills as $k => $item) {
                Redis::hset("seckilllist", $item['id'], json_encode($item));
                Redis::set("seckill" . $item['id'], $item['stock']);
            }
            return $seckills;
        } else {
            foreach (Redis::hgetall('seckilllist') as $item) {
                $redislist[] = json_decode($item, true);
            }
            return $redislist;
        }
    }

    //实时热抢
    static public function hotRush($allow, $where)
    {
        $time = (new BaseController())->NowTime();
        return Seckill::dataSorting($allow, $where, $time);
    }

    //秒杀商品
    static public function OnceSeckill($id, $allow_field = null)
    {
        $good_id = Seckill::skInGood($id, 'goods_id');
        //获取商品信息
        $data = Goods::OnceGood($good_id, $allow_field);
        $data['goods_image'] = config('aliyun.cui') . $data['goods_image'];
        $data['discounted_price'] = Seckill::skInGood($id, 'price');
        $data['end_time'] = Seckill::skInGood($id, 'end_time');
        return $data;
    }

    //获得评论
    static public function getComments($good_id)
    {
        $comment = [];
        $video = [];
        $pictures = [];
        $addendum = [];
        $array_cid = GoodsComments::allComments($good_id);
        foreach ($array_cid as $item) {
            $comment[] = Comments::getAllComment($item['comment_id']);
        }
        foreach ($comment as &$v) {
            $v['goods_image'] = config('aliyun.cui') . $v['goods_image'];
            $v['avatar'] = config('aliyun.cui') . $v['avatar'];
            if ($v['detail_content'] == 1) {
                $video[] = $v;
            } else if ($v['detail_content'] == 2) {
                $pictures[] = $v;
            } else {
                $addendum[] = $v;
            }
        }
        return [
            'all' => $comment,
            'video' => ['data' => $video, 'count' => count($video)],
            'pictures' => ['data' => $pictures, 'count' => count($pictures)],
            'addendum' => ['data' => $addendum, 'count' => count($addendum)]
        ];
    }

    //时间区间
    static public function getTimeSk(string $time, array $allow)
    {
        $equivalent = self::equivalent();
        $days = (new CommonController())::nowDay($time);
        if ($equivalent) {
            foreach (Redis::hvals('seckilllist') as $v) {
                $item = json_decode($v, true);
                if (strstr($item['start_time'], $days)) {
                    $seckill[] = $item;
                }
            }
            return $seckill;
        } else {
            return Seckill::timeInterval($days, $allow);
        }
    }

    //进行秒杀
    static public function goSeckill($uid, $seckill_id)
    {
        //获得队列长度
        $count = Redis::get('seckill' . $seckill_id);
        //获取秒杀结束时间
        $endtime = Seckill::skInGood($seckill_id, 'end_time');
        if (strtotime($endtime) < time()) {
            return false;
        }
        //判断是否可以进入队列
        if (Redis::llen('seckill' . $seckill_id . 'list') < $count) {
            //实例化Redis锁
            $lock = new ServicesSeckill();
            return $lock->user($seckill_id, $uid);
        } else {
            return false;
        }
    }

    //生成订单
    static public function generateOrder($orderdata)
    {
        return SeckillOrder::addOrder($orderdata);
    }

    //秒杀订单查询
    static public function seckillOrder($uid)
    {
        return SeckillOrder::getOrder($uid);
    }

    //获得商品价钱
    static public function getSeckillInfo($seckill_id)
    {
        //根据秒杀ID查询商品ID
        $goods = Seckill::orderInfo($seckill_id);
        $goods['goods_price'] = Goods::OnceGood($goods['goods_id'], 'price')['price'];
        return $goods;
    }

}
